package example.com.terry.servlet;

import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 日程增加接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/insertScheduleServlet")
public class InsertScheduleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (CookieUtils.isSign(req)) {
            // todo 如果是登陆状态
            String scheduleText = req.getParameter("scheduleText");
            String scheduleDate = req.getParameter("scheduleDate");
            String scheduleEnd = req.getParameter("scheduleEnd");
            String id = req.getParameter("id");
            String type = req.getParameter("type");
            if (id != null) {
                String sql = "update tb_schedule set schedule_text = '" + scheduleText + "', type = '" + type + "' ,create_time ='" + scheduleDate + "',schedule_end = '" + scheduleEnd + "' where id=" + id;

                if (JDBCUtils.init(sql)) {
                    //
                    resp.getWriter().println("修改日程记录成功");
                } else {
                    resp.getWriter().println("修改日程记录失败");
                }
            } else {
                String user = CookieUtils.getUser(req);
                String sql = "insert into tb_schedule(schedule_text,schedule_date,type,user_id,create_time,schedule_end) values('" + scheduleText + "','" + scheduleDate + "','" + type + "','" + user + "',now(),'" + scheduleEnd + "' )";
                boolean init = JDBCUtils.init(sql);
                if (init) {
                    //
                    resp.getWriter().println("增加日程记录成功");
                } else {
                    resp.getWriter().println("增加日程记录失败");
                }
            }

        } else {
            // 非登陆状态
        }
    }
}
